package com.wuhan.template.module.home.activity;

import android.support.annotation.NonNull;
import android.widget.ListView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.wuhan.template.R;
import com.wuhan.template.base.BaseActivity;
import com.wuhan.template.constant.Urls;
import com.wuhan.template.module.home.adapter.DianBoBaseAdapter;
import com.wuhan.template.module.home.entity.DianBoEntity;
import com.wuhan.template.utils.EventMessage;
import com.wuhan.template.utils.OkHttpUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

public class DianBoActivity extends BaseActivity {

    @BindView(R.id.dianbo_pullListView)
    protected PullToRefreshListView listView;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_dian_bo;
    }

    @Override
    protected void initViews() {
       /* DianBoBaseAdapter boBaseAdapter = new DianBoBaseAdapter(this,dianBoLists);
        listView.setAdapter(boBaseAdapter);*/

        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                dianBoLists.clear();
                loadData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

            }
        });
    }

    @Override
    protected void loadData() {
        OkHttpUtils.get(Urls.DIANBo,null);
    }

    private  List<DianBoEntity> dianBoLists = new ArrayList<>();;
    @Override
    protected void postEventResult(@NonNull EventMessage message) {

        if(message.type.equals(Urls.DIANBo)){
            Gson gson = new Gson();
            DianBoEntity[] dianBoEntities = gson.fromJson(message.result, DianBoEntity[].class);

            dianBoLists.addAll(Arrays.asList(dianBoEntities));
            DianBoBaseAdapter boBaseAdapter = new DianBoBaseAdapter(this,dianBoLists);
            listView.setAdapter(boBaseAdapter);
            listView.onRefreshComplete();
        }
    }
}
